import type { DishwasherController } from '@/controller/Dishwasher/definition'
import i18n from '@/i18n'

export function useStorage(control: DishwasherController.Control) {
  //抑菌储存时间
  const storageActiveTime = computed(() => control.store.detail?.storageActiveTime as number)
  //是否显示抑菌储存进度条
  const isShowStorage = computed(() => control.store.isStorage)
  //抑菌存储时间百分比
  const storagePercent = ref(0)
  //抑菌存储最终显示值
  const storageActiveTimeValue = ref('')
  watch(
    () => storageActiveTime.value,
    (value) => {
      if (!isShowStorage) return
      getStorageTime(value)
    }
  )
  /**
   * 返回储存最终显示文本
   * @returns
   */
  function getStorageTime(value: number) {
    if (value < 60) {
      storagePercent.value = 0
      storageActiveTimeValue.value = i18n.global.t('Dishwasher.BacteriostatStorage.StorageFull.Text')
    } else if (value >= 345600) {
      storagePercent.value = 100
      storageActiveTimeValue.value = i18n.global.t('Dishwasher.BacteriostatStorage.StorageFull.Text')
    } else {
      let time = formatHourMinute(value)
      storagePercent.value = parseInt(String((value / 345600) * 100))
      storageActiveTimeValue.value = `${i18n.global.t('Dishwasher.BacteriostatStorage.Storaging.Text')}${time}`
    }
  }
  /**
   * 将时间戳转为时分
   * @param value
   * @returns
   */
  function formatHourMinute(value: number) {
    var secondTime = value // 秒
    var minuteTime = 0 // 分
    var hourTime = 0 // 小时
    if (secondTime > 59) {
      minuteTime = secondTime / 60
      secondTime = secondTime % 60
      if (minuteTime > 59) {
        hourTime = minuteTime / 60
        minuteTime = minuteTime % 60
      }
    }
    var result = ''
    if (parseInt(minuteTime + '') > 0) {
      result = '' + parseInt(minuteTime + '') + '分钟' + result
    }
    if (parseInt(hourTime + '') > 0) {
      result = '' + parseInt(hourTime + '') + '小时' + result
    }
    return result
  }

  return {
    isShowStorage,
    storagePercent,
    storageActiveTimeValue,
  }
}
